<#import "../common/layout.ftl" as layout/>
<#import "../common/dataTables.ftl" as dataTables/>
<#import "../common/modern-forms.ftl" as mform/>
<#import "../common/modal.ftl" as modal/>

<!DOCTYPE html>
<html>
<@layout.head title="接口管理">
    <@dataTables.head/>
    <@dataTables.buttons_head/>
    <@dataTables.select_head/>
    <@dataTables.fixedHeader_head/>
    <@mform.head/>
    <@select.head/>
</@layout.head>
<body>
<@layout.page>
    <div class="main-box">

        <!-- 表格 -->
        <div class="content">
            <!-- 按钮 -->
            <div id="mUserBtnGroup" class="btn-group">
                <button type="button" class="btn btn-blue btn-sm"
                        onclick="javascript:refreshData();">
                    <i class="fa fa-refresh"></i> 刷新
                </button>
                <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                        onclick="javascript:newServer();">
                    <i class="fa fa-plus"></i> 新增
                </button>
                <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                        onclick="javascript:editServer();">
                    <i class="fa fa-edit"></i> 修改
                </button>
                <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                        onclick="javascript:delServer();">
                    <i class="fa fa-minus"></i> 删除
                </button>
                <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                        onclick="javascript:enterprise();">
                    <i class="fa fa-plus"></i> 接口配置
                </button>
                <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                        onclick="javascript:interfaceLog();">
                    <i class="fa fa-plus"></i> 接口日志
                </button>
            </div>
            <!-- 表格 -->
            <@dataTables.commonTable id="mainTable"/>
        </div>
    </div>
    <!-- 弹出窗 -->
    <@modal.editModal id="modal-mUser" buttonId="btnmUserSave" width="1000px" height="800">
        <@mform.container id="userEditForm">
            <input id="id" name="id" type="hidden"/>
            <input id="ServerId" name="ServerId" type="hidden"/>
            <@mform.row>
                <@mform.input_text id="name" name="name" size=6
                label="名称" placeholder="请输入" icon="fa-edit" />
                <@mform.input_text id="code" name="code" size=6
                label="interfaceCode" placeholder="请输入" icon="fa-edit" />


                <@mform.input_text id="productionURL" name="productionURL" size=6
                label="生产URL" placeholder="请输入" icon="fa-edit" />
                <@mform.input_text id="testURL" name="testURL" size=6
                label="测试URL" placeholder="请输入" icon="fa-edit" />
                <@mform.select2 id='currentPattern' name='currentPattern' label="当前模式" size=4 >
                    <#list currentPattern as item>
                        <option value="${item.value}">${item.text}</option>
                    </#list>
                </@mform.select2>
                <@mform.select2 id='logWay' name='logWay' label="日志方式" size=4>
                    <#list logWay as item>
                        <option value="${item.value}">${item.text}</option>
                    </#list>
                </@mform.select2>

                <@mform.select2 id='unit' name='unit' label="上限单位" size=4 >
                    <#list unit as item>
                        <option value="${item.value}">${item.text}</option>
                    </#list>
                </@mform.select2>
                <@mform.input_text id="sumCount" name="sumCount" size=4
                label="总次数上限" placeholder="请输入" icon="fa-edit" />
                <@mform.input_text id="customerCount" name="customerCount" size=4
                label="用户次数上限" placeholder="请输入" icon="fa-edit" />
                <@mform.select2 id='state' name='state' label="接口状态" size=4 >
                    <#list state as item>
                        <option value="${item.value}">${item.text}</option>
                    </#list>
                </@mform.select2>
                <@mform.input_textarea id="testOutputMessage" name="testOutputMessage" size=12
                label="测试输出报文" placeholder="请输入"  />
                <@mform.input_textarea id="testReturnMessage" name="testReturnMessage" size=12
                label="测试返回报文" placeholder="请输入"  />

            </@mform.row>

        </@mform.container>
    </@modal.editModal>

</@layout.page>
<@layout.foot>
    <@dataTables.foot/>
    <@dataTables.buttons_foot/>
    <@dataTables.select_foot/>
    <@dataTables.fixedHeader_foot/>
    <@mform.foot/>
    <script src="${request.contextPath}/assets/js/jquery.serializejson.min.js?v=3653060703"></script>
    <script type="text/javascript">
        var currentPatternMap = {
            <#list currentPattern as item>
            "${item.value}": "${item.name}",
            </#list>
        }
        var stateMap = {
            <#list state as item>
            "${item.value}": "${item.name}",
            </#list>
        }
        var logWayMap = {
            <#list logWay as item>
            "${item.value}": "${item.name}",
            </#list>
        }
        var unitMap = {
            <#list unit as item>
            "${item.value}": "${item.name}",
            </#list>
        }
        var columns =
            [

                {"sWidth": "130px","title": "名称", "data": "name"},
                {"sWidth": "80px","title": "interfaceCode", "data": "code"},
                {"sWidth": "80px","title": "生产地址", "data": "productionURL"},
                {"sWidth": "80px","title": "测试地址", "data": "testURL"},
                {"sWidth": "45px","title": "当前模式", "data": "currentPattern"},
                {"sWidth": "45px","title": "日志方式", "data": "logWay"},
                {"sWidth": "40px","title": "上限单位", "data": "unit"},
                {"sWidth": "65px","title": "上限值", "data": "sumCount"},
                //{"sWidth": "75px","title": "用户次数上限", "data": "customerCount"},
                {"sWidth": "110px","title": "新增时间", "data": "createTime"}


            ];
        var mainTable = $('#mainTable').winningTable({
            columns: columns,
            pageLength: 10,
            toolbar: "#mUserBtnGroup",
            /*select: {style: 'multi'},*/
            fixedHeader: false,
            searching: true,
            ordering: true,
            buttons: [

            ],
            "rowCallback": function (row, data, index) {
                $('td', row).eq(6).html("总次数："+(data.sumCount||"无")+"<br>用户次数："+(data.customerCount||"无"));
                $('td', row).eq(11).html( stateMap[data.state] );
                $('td', row).eq(3).html( currentPatternMap[data.currentPattern] );
                $('td', row).eq(4).html( logWayMap[data.logWay] );
                $('td', row).eq(5).html( unitMap[data.unit] );
                /*if (data.validFlag == 100001) {
                    $('td:eq(7)', row).html('有效');
                } else {
                    $('td:eq(7)', row).html('无效');
                }*/
            }
        });
        function newServer() {
            $('#modal-mUser').find(".modal-title").text('新增接口信息');
            clearForm($('#modal-mUser'));
            $("#btnmUserSave").unbind();
            $("#btnmUserSave").click(saveServerNew);
            $('#modal-mUser').modal();
        }
        function editServer() {
            var datas = mainTable.getSelectedDatas();
            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            var $modal = $("#modal-mUser");
            $modal.find(".modal-title").text("修改接口信息");
            clearForm($modal);
            var data = datas[0];
            initFormItemData($("#userEditForm"), data);
            $modal.modal();
            $("#btnmUserSave").unbind();
            $("#btnmUserSave").click(saveServerNew);

        }

        function refreshData() {
            mainTable.queryData("${request.contextPath}/interface/queryAll/"+${serverId});
        }
        refreshData()
        function delServer() {
            var datas = mainTable.getSelectedDatas();
            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            var data = datas[0];
            swal({
                title: "确定删除",
                text: "是否确定删除此数据",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的,确定",
                cancelButtonText: "取消"
            }).then(function (result) {
                if(!result.value)
                    return;
                var requestData = {
                    id:data.id
                };
                var params = {datas: JSON.stringify(requestData)};
                $.ajax({
                    type: 'POST',
                    data: params,
                    url: '${request.contextPath}/interface/delete',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            }).then(function (result) {
                                refreshData();
                            });
                        }
                        else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            });
        }

        function saveServerNew() {

            swal({
                title: "确定新增",
                text: "是否确定新增此数据",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的,确定",
                cancelButtonText: "取消"
            }).then(function (result) {
                if (!result.value)
                    return;
                var requestData = $("#modal-mUser form").serializeJSON();
                var params = {datas: JSON.stringify(requestData)};
                $.ajax({
                    type: 'POST',
                    data: params,
                    url: '${request.contextPath}/interface/save',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            }).then(function (result) {
                                $("#modal-mUser").modal('hide');
                                refreshData();
                            });
                        } else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            });
        }
        //接口配置
        function enterprise() {
            var datas = mainTable.getSelectedDatas();

            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            var data = datas[0];
            top.addTabs({
                id: "inter"+data.id,
                title: "接口配置",
                close: 'ture',
                url: "${request.contextPath}/interface/interfaceM/" + data.id
            })
        }
        //接口配置
        function interfaceLog() {
            var datas = mainTable.getSelectedDatas();

            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            var data = datas[0];
            top.addTabs({
                id: "interfaceLog"+data.id,
                title: "接口日志",
                close: 'ture',
                url: "${request.contextPath}/interface/interfaceLog/" + data.id
            })
        }
    </script>
</@layout.foot>
</body>
</html>